package com.shwread.android.qysw.activity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ViewGroup;

import com.humanres.activity.ResMoreActivity;
import com.mywork.WorkUIKit;
import com.netease.nim.demo.session.activity.P2PMessageActivity;
import com.netease.nim.demo.session.activity.TeamMessageActivity;
import com.netease.nimlib.sdk.NimIntent;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shwread.android.qysw.PushMessageService;
import com.shwread.android.qysw.step.HealthyConfig;
import com.shwread.android.qysw.step.PedometerSettings;
import com.shwread.android.qysw.step.StepService;
import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.page.MainTabPage;
import com.shwread.android.ui.page.MyWorkPage;
import com.shwread.android.ui.page.PersonalInfoPage;
import com.shwread.android.ui.page.SplashPage;
import com.tgx.tina.android.ipc.framework.IUIPage;
import com.tgx.tina.android.task.ATaskService;
import com.ui.dialog.CustomDialog;

import org.geometerplus.android.util.QyreaderPerferencesUtil;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import logic.bean.BannerInfo;
import logic.bean.BookBlock;
import logic.bean.BookInfo;
import logic.bean.EBannerInfo;
import logic.bean.MessageInfo;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.util.Util;


public class MainActivity
        extends
        BaseBusinessActivity {
    public static final String TO_ENTERPRISE_ACTION = "com.dracom.action.EnterprisePage";
    public static final String TO_BOOKSHELF_ACTION = "com.dracom.action.BookShelfPage";
    public static MainActivity instance = null;
    private QyreaderPerferencesUtil perferencesUtil;
    private StepService mService;
    private int mStepValue;
    private SharedPreferences mSettings;
    private PedometerSettings mPedometerSettings;
    /**
     * True, when service is running.
     */
    private boolean mIsRunning;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (curPage != null) curPage.notifyView(msg.what, msg.getData());
            if (msg.what == DefaultConsts.LOGIN_OK) {
                getSyncData();
            }
        }
    };

    @Override
    public Handler getUIHandler() {
        return handler;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBannerWH();
        mAService = new ATaskService();
        mAService.startAService(this);
        instance = this;
        startPushMessageService();
        //绑定手机识别码提示框
        if (getIntent().hasExtra("code") && getIntent().getIntExtra("code", 0) == 1) {
            CustomDialog.Builder builder = new CustomDialog.Builder(this);
            builder.setMessage("欢迎您使用" + getResources().getString(R.string.app_name) + "，您的移动设备已与登录帐号绑定");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
        if (TO_ENTERPRISE_ACTION.equals(getIntent().getAction())) {
            ChangeTabPage(MainTabPage.TAG_TAB1);

        } else if (TO_BOOKSHELF_ACTION.equals(getIntent().getAction())) {
            ChangeTabPage(MainTabPage.TAG_TAB2);
        } else {
            changePage(new SplashPage(this), IUIPage.KEEP_LAST_STATUS);
        }

        if (Util.isNotEmpty(Const.phone_number)) {
            perferencesUtil = QyreaderPerferencesUtil.getInstance(this, Const.phone_number);
            perferencesUtil.setExistMainActivity(true);
            perferencesUtil.setLastPushMessageId(0L);
        }

        sendBroadcast();
        mSettings = PreferenceManager.getDefaultSharedPreferences(this);
        mPedometerSettings = new PedometerSettings(mSettings);
        mIsRunning = mPedometerSettings.isServiceRunning();
        if (HealthyConfig.isUseStep) {
            startStepService();
        }

        mIsRunning = true;

        mPedometerSettings.clearServiceRunning();
        //		new TaiBaoLogin().taibaoLogin(this, Const.phone_number);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        onParseIntent();
    }

    private void onParseIntent() {
        Intent intent = getIntent();
        if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
//			IMMessage message = (IMMessage) getIntent().getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
            ArrayList<IMMessage> messages = (ArrayList<IMMessage>) getIntent().getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
            switch (messages.get(0).getSessionType()) {
                case P2P:
                    P2PMessageActivity.start(this, messages.get(0).getSessionId());
                    break;
                case Team:
                    TeamMessageActivity.start(this, messages.get(0).getSessionId());
                    break;
                default:
                    break;
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = ((StepService.StepBinder) service).getService();
            mService.registerCallback(mCallback);
            mService.reloadSettings();

        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

    private void resetValues(boolean updateDisplay) {
        if (mService != null && mIsRunning) {
            mService.resetValues();
        } else {
            SharedPreferences state = this.getSharedPreferences("state", 0);
            SharedPreferences.Editor stateEditor = state.edit();
            if (updateDisplay) {
                stateEditor.putInt("steps", 0);
                stateEditor.putInt("pace", 0);
                stateEditor.putFloat("distance", 0);
                stateEditor.putFloat("speed", 0);
                stateEditor.putFloat("calories", 0);
                stateEditor.apply();
            }
        }
    }

    private void startStepService() {
        if (!mIsRunning) {
            android.util.Log.i("darren-------", "startStepService");
            android.util.Log.i("darren", "[SERVICE] Start");
            mIsRunning = true;
            this.startService(new Intent(this, StepService.class));
        }
    }


    private void stopStepService() {
        android.util.Log.i("darren", "[SERVICE] Stop");
        if (mService != null) {
            android.util.Log.i("darren-------", "stopStepService");
            android.util.Log.i("darren", "[SERVICE] stopService");
            this.stopService(new Intent(this, StepService.class));
        }
        mIsRunning = false;
    }

    private void sendBroadcast() {
        QyreaderPerferencesUtil util = QyreaderPerferencesUtil.getInstance(this, Const.phone_number);
        Intent intent = new Intent(DefaultConsts.PUSHMESSAGE_BROADCAST);
        intent.putExtra("vibrate", util.getShowZhendong());
        intent.putExtra("notice", util.getShowNotice());
        intent.putExtra("pushMessage", util.getPushMessage());
        intent.putExtra(DefaultConsts.LAST_PUSH_MESSAGE_ID, util.getLastPushMessageId());
        sendBroadcast(intent);
    }

    void ChangeTabPage(int type) {
        if (MainTabPage.getInstance(this).getView() != null) {
            ViewGroup group = (ViewGroup) MainTabPage.getInstance(this).getView().getParent();
            group.removeAllViews();
            MainTabPage page = MainTabPage.getInstance(this);
            page.setPageSelected(type);
            changePage(page, IUIPage.KEEP_LAST_STATUS);

            // startService();
        } else {
            MainTabPage page = MainTabPage.getInstance(this);
            page.setPageSelected(type);
            changePage(page, IUIPage.KEEP_LAST_STATUS);
            // startService();
        }
    }

    private void initBannerWH() {
        DefaultConsts.BANNER_DEFAULT_WIDTH = 640;
        DefaultConsts.BANNER_DEFAULT_HIGHT = 260;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (curPage != null) curPage.notifyView(DefaultConsts.PAGE_ONRESUME_CODE, null);
        if (MyWorkPage.getInstance(this).getView() != null && WorkUIKit.getUser() != null) {
            MyWorkPage.getInstance(this).setRefresh();
            Log.i("onRestart", "onRestart");
        }
    }

    private boolean exit;

    public void needExit() {
        exit = true;
    }

    @Override
    protected void onDestroy() {
//        if(HealthyConfig.isUseStep){
//            stopStepService();
//        }
        if (mAService != null) {
            mAService.stopAService();
        }
        super.onDestroy();
        if (exit) {
            System.exit(0);
        }
    }

    @Override
    public void onBackPressed() {
        if (curPage != null && curPage.handleBack()) {
            return;
        }
        if (history.isEmpty()) {
            finish();
        }
        onBackPressed0();
    }

    public void goPersonalActivity() {
        Intent intent = new Intent(this, PersonalInfoActivity.class);
        startActivity(intent);
    }

    public void goColumnDetailActivity() {
        Intent intent = new Intent(this, ColumnDetailActivity.class);
        intent.putExtra("journal", true);
        startActivity(intent);
    }

    public void goColumnDetailActivity(Bundle b) {
        Intent intent = new Intent(this, ColumnDetailActivity.class);
        intent.putExtra("bundle", b);
        startActivity(intent);
    }

    public void goColumnDetailActivity(BookBlock block) {
        Intent intent = new Intent(this, ColumnDetailActivity.class);
        intent.putExtra("blockId", block.getBlockId());
        intent.putExtra("blockTypeId", block.getBlockTypeId());
        intent.putExtra("blockTypeName", block.getBlockTypeName());
        startActivity(intent);
    }

    public void goWebViewActivity(MessageInfo info) {
        Intent webIntent = new Intent(this, CommonWebActivity.class);
        // if (info.getIsRead().toLowerCase().equals("n"))
        // {
        // webIntent.putExtra(DefaultConsts.message_view_id,
        // info.getMsgViewId());
        // }
        webIntent.putExtra(DefaultConsts.title_s, "消息内容");
        webIntent.putExtra(DefaultConsts.req_url_s, info.getAddressUrl());
        webIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(webIntent);
    }

    public void goWebViewActivity(EBannerInfo info) {
        Intent webIntent = new Intent(this, CommonWebActivity.class);
        webIntent.putExtra(DefaultConsts.title_s, info.getTitle());
        webIntent.putExtra(DefaultConsts.req_url_s, info.getBannerUrl());
        webIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(webIntent);
    }

    public void goWebViewActivity(BannerInfo info) {
        Intent webIntent = new Intent(this, GameWebActivity.class);
        webIntent.putExtra(DefaultConsts.title_s, info.getBannerName());
        webIntent.putExtra(DefaultConsts.req_url_s, info.getContent());
        webIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(webIntent);
    }

    public void goWebViewActivity(Bundle b) {
        Intent webIntent = new Intent(this, CommonWebActivity.class);
        webIntent.putExtra("bundle", b);
        webIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(webIntent);
    }

    public void goEnterpriseShelfColumn() {
        startActivity(new Intent(this, EnterpriseShelfColumn.class));
    }

    public void goSystemNotiActivity(MessageInfo info) {
        Intent intent = new Intent(this, SystemNotiActivity.class);
        // if (info.getIsRead().toLowerCase().equals("n"))
        // {
        // intent.putExtra(DefaultConsts.message_view_id, info.getMsgViewId());
        // }
        intent.putExtra(DefaultConsts.title_s, "消息内容");
        intent.putExtra(DefaultConsts.message_content_s, info.getMsgContent());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public void goResMoreActivity(String title) {
        Intent intent = new Intent(this, ResMoreActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("push", true);
        startActivity(intent);
    }

    public void goBookDetailActivity(BookInfo bean) {
        // #ifdef SHOW_OLDBOOKDETAIL
        // $ Intent intent = new Intent(this, BookDetailActivity.class);
        // #else
        Intent intent = new Intent(this, NewBookDetailActivity.class);
        // #endif
        intent.putExtra("BookInfo", bean);
        startActivity(intent);
    }

    public void goBookDetailActivity(List<BookInfo> list) {
        if (list != null && list.get(0) != null) {
            // #ifdef SHOW_OLDBOOKDETAIL
            // $ Intent bookIntent = new Intent(this, BookDetailActivity.class);
            // #else
            Intent bookIntent = new Intent(this, NewBookDetailActivity.class);
            // #endif

            bookIntent.putExtra("BookInfo", list.get(0));
            startActivity(bookIntent);
        } else {
            Util.showToast(this, "书籍已下架");
        }
    }

    // public void startService() {
    // Intent serviceIntent = new Intent(this, PushService.class);
    // serviceIntent.putExtra("account", Const.phone_number);
    // serviceIntent.putExtra("key", Const.login_key);
    // LogUtil.fw_d("__________客户端key" + Const.login_key);
    // //
    // serviceIntent.putExtra(com.tgx.tina.android.ipc.framework.DefaultConsts.serviceAction,
    // Intent.ACTION_DEFAULT);
    // startService(serviceIntent);
    // }

    public void startPushMessageService() {
        Intent pushMessageService = new Intent(this, PushMessageService.class);
        startService(pushMessageService);
    }

	/*
     * @Override
	 * protected void onActivityResult(int requestCode, int resultCode, Intent
	 * data) {
	 * super.onActivityResult(requestCode, resultCode, data);
	 * if (RESULT_OK != resultCode) return;
	 * Bundle bdata = null;
	 * if (data != null)
	 * {
	 * bdata = data.getBundleExtra(BUNDLE_DATA);
	 * }
	 * curPage.notifyView(requestCode, bdata);
	 * }
	 */

    @SuppressLint("NewApi")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //if (RESULT_OK != resultCode) return;
        switch (requestCode) {
            case Const.CAPTURE_IMAGE:
                if (RESULT_OK != resultCode) return;
                Intent intent = new Intent("com.android.camera.action.CROP");
                Bundle extras = new Bundle();
                extras.putString("crop", "true");
                extras.putInt("aspectX", 1);
                extras.putInt("aspectY", 1);//这里的File文件可以是通过系统相册选取的，也可以是位于存储卡中任何一副图片文件
                intent.putExtra("outputX", 120);
                intent.putExtra("outputY", 120);
                intent.setDataAndType(Uri.fromFile(PersonalInfoPage.mImageFile), "image/jpeg");
                intent.putExtras(extras);
                intent.putExtra("noFaceDetection", true);
                intent.putExtra("return-data", true);
                startActivityForResult(intent, Const.CROP_IMAGE);
                break;
            case Const.GET_LOCAL_IMAGE:
                if (data == null) {
                    return;
                }
                Uri uri;
                uri = data.getData();
                String filePath = uri.getPath();
                if (!(filePath.endsWith(".jpg") || filePath.endsWith(".jpeg") || filePath.endsWith(".png") || filePath.endsWith(".bmp") || filePath.endsWith(".gif") || filePath.startsWith("/external/images"))) {
                    if (isDocumentUri(MainActivity.this, uri)) {
                        String wholeID = getDocumentId(uri);
                        String id = wholeID.split(":")[1];
                        String[] column = {
                                MediaStore.Images.Media.DATA
                        };
                        String sel = MediaStore.Images.Media._ID + "=?";
                        Cursor cursor = MainActivity.this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{
                                id
                        }, null);
                        int columnIndex = cursor.getColumnIndex(column[0]);
                        if (cursor.moveToFirst()) {
                            filePath = cursor.getString(columnIndex);
                        }
                        cursor.close();
                    } else {
                        String[] projection = {
                                MediaStore.Images.Media.DATA
                        };
                        Cursor cursor = MainActivity.this.getContentResolver().query(uri, projection, null, null, null);
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();
                        filePath = cursor.getString(column_index);
                    }
                    if (!(filePath.endsWith(".jpg") || filePath.endsWith(".jpeg") || filePath.endsWith(".png") || filePath.endsWith(".bmp") || filePath.endsWith(".gif") || filePath.startsWith("/external/images"))) {
                        Util.showToast(this, "请选择JPG PNG BMP GIF格式的图片文件");
                        return;
                    }
                    uri = Uri.fromFile(new File(filePath));
                }
                if (uri == null) return;
                Intent intent1 = new Intent("com.android.camera.action.CROP");
                intent1.setDataAndType(uri, "image/jpeg");//data是图库选取文件传回的参数
                intent1.putExtra("crop", "true");
                intent1.putExtra("aspectX", 1);
                intent1.putExtra("aspectY", 1);
                intent1.putExtra("outputX", 120);
                intent1.putExtra("outputY", 120);
                intent1.putExtra("noFaceDetection", true);
                intent1.putExtra("return-data", true);
                startActivityForResult(intent1, Const.CROP_IMAGE);
                break;
            case Const.CROP_IMAGE:
                if (data == null) return;
                goCropImage(requestCode, data);
                break;
            case Const.ATTEND_REACH:
                if (data==null) {
                    Bundle outextras = data.getExtras();
                    Message msg = Message.obtain();
                    msg.setData(outextras);
                    msg.what = DefaultConsts.MSG_OUT_REMARK;
                    handler.sendMessage(msg);
                }


//                if (resultCode == 1000) {
//                    Bundle outextras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(outextras);
//                    msg.what = DefaultConsts.MSG_OUT_REMARK;
//                    handler.sendMessage(msg);
//                } else if (resultCode == 1001) {
//                    Bundle leaveextras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(leaveextras);
//                    msg.what = DefaultConsts.MSG_LEAVE_REMARK;
//                    handler.sendMessage(msg);
//                } else if (resultCode == 1002) {
//                    Bundle otherextras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(otherextras);
//                    msg.what = DefaultConsts.MSG_OTHER_REMARK;
//                    handler.sendMessage(msg);
//                }



                break;
            case Const.ATTEND_QUIT:
                if (data==null) {
                    Bundle outextras2 = data.getExtras();
                    Message msg2 = Message.obtain();
                    msg2.setData(outextras2);
                    msg2.what = DefaultConsts.MSG_OUT_BACKREMARK;
                    handler.sendMessage(msg2);
                }
//                if (resultCode == 1000) {
//                    Bundle quitOutExtras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(quitOutExtras);
//                    msg.what = DefaultConsts.MSG_OUT_BACKREMARK;
//                    handler.sendMessage(msg);
//                } else if (resultCode == 1001) {
//                    Bundle quitLeaveExtras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(quitLeaveExtras);
//                    msg.what = DefaultConsts.MSG_LEAVE_BACKREMARK;
//                    handler.sendMessage(msg);
//                } else if (resultCode == 1002) {
//                    Bundle quitOtherExtras = data.getExtras();
//                    Message msg = Message.obtain();
//                    msg.setData(quitOtherExtras);
//                    msg.what = DefaultConsts.MSG_OTHER_BACKREMARK;
//                    handler.sendMessage(msg);
//                }
                break;
            default:
                Bundle bdata = null;
                if (data != null) {
                    bdata = data.getBundleExtra(BUNDLE_DATA);
                }
                curPage.notifyView(requestCode, bdata);
                break;
        }
    }

    public static String getDocumentId(Uri uri) {
        String res = null;
        try {
            Class<?> c = Class.forName("android.provider.DocumentsContract");
            Method get = c.getMethod("getDocumentId", Uri.class);
            res = (String) get.invoke(c, uri);
        } catch (Exception ignored) {
            ignored.getMessage();
        }
        return res;
    }

    public static boolean isDocumentUri(Context context, Uri uri) {
        Boolean res = false;
        try {
            Class<?> c = Class.forName("android.provider.DocumentsContract");
            Method get = c.getMethod("isDocumentUri", Context.class, Uri.class);
            res = (Boolean) get.invoke(c, context, uri);
        } catch (Exception ignored) {
            ignored.getMessage();
        }
        return res;
    }

    /**
     * 裁切图片
     */
    private void goCropImage(int requestCode, Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Message msg = Message.obtain();
            msg.setData(extras);
            msg.what = DefaultConsts.PERSONAL_UPDATEUI_FACE_UPDATE_HEAD;
            handler.sendMessage(msg);
        }
    }

    // TODO: unite all into 1 type of message
    private StepService.ICallback mCallback = new StepService.ICallback() {
        public void stepsChanged(int value) {
            android.util.Log.i("darren", "stepChanged");
            mHandler.sendMessage(mHandler.obtainMessage(STEPS_MSG, value, 0));
        }

        public void paceChanged(int value) {
            mHandler.sendMessage(mHandler.obtainMessage(PACE_MSG, value, 0));
        }

        public void distanceChanged(double value) {
            mHandler.sendMessage(mHandler.obtainMessage(DISTANCE_MSG, (int) (value * 1000), 0));
        }

        public void speedChanged(float value) {
            mHandler.sendMessage(mHandler.obtainMessage(SPEED_MSG, (int) (value * 1000), 0));
        }

        public void caloriesChanged(double value) {
            mHandler.sendMessage(mHandler.obtainMessage(CALORIES_MSG, (int) value, 0));
        }
    };
    private static final int STEPS_MSG = 1;
    private static final int PACE_MSG = 2;
    private static final int DISTANCE_MSG = 3;
    private static final int SPEED_MSG = 4;
    private static final int CALORIES_MSG = 5;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case STEPS_MSG:
                    mStepValue = msg.arg1;
                    String text = String.valueOf(mStepValue);
                    android.util.Log.i("darren", "mStepValue" + text);
                    // 设置参数

                    break;
                case PACE_MSG:
                    //                    mPaceValue = msg.arg1;
                    //                    if (mPaceValue <= 0) {
                    //                        mPaceValueView.setText("0");
                    //                    } else {
                    //                        mPaceValueView.setText("" + (int) mPaceValue);
                    //                    }
                    break;
                case DISTANCE_MSG:
                    //                    mDistanceValue = ((int) msg.arg1) / 1000f;
                    //                    if (mDistanceValue <= 0) {
                    //                        tvDistance.setText("0");
                    //                    } else {
                    //                        tvDistance.setText(
                    //                                ("" + (mDistanceValue + 0.000001f)).substring(0, 5)
                    //                        );
                    //                    }
                    break;
                case SPEED_MSG:
                    //                    mSpeedValue = ((int) msg.arg1) / 1000f;
                    //                    if (mSpeedValue <= 0) {
                    //                        mSpeedValueView.setText("0");
                    //                    } else {
                    //                        mSpeedValueView.setText(
                    //                                ("" + (mSpeedValue + 0.000001f)).substring(0, 4)
                    //                        );
                    //                    }
                    break;
                case CALORIES_MSG:
                    //                    mCaloriesValue = msg.arg1;
                    //                    if (mCaloriesValue <= 0) {
                    //                        mCaloriesValueView.setText("0");
                    //                    } else {
                    //                        mCaloriesValueView.setText("" + (int) mCaloriesValue);
                    //                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }

    };
}